package cnhy.salary.service;

import cnhy.salary.domain.*;
import cnhy.salary.domain.dto.EmployeeDto;
import cnhy.salary.domain.dto.lineMonthlyWageDto;
import cnhy.salary.domain.proxy.DailyWageProxy;
import com.easy.query.core.proxy.sql.GroupKeys;
import com.easy.query.core.proxy.sql.Select;
import ling.common.EQueryService;
import ling.common.domain.PageSort;
import ling.common.domain.Result;
import ling.common.domain.ResultRows;
import lombok.var;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class LineSvc extends EQueryService<ProductionLine> {

    //产线岗位：班组,岗位,类型,系数,工资,岗位补贴,操作
    public ResultRows<PostCfg>  linePosts(Long lineId, PageSort pageSort) {
        var emplList = queryHelper.queryable(EmployeeDto.class)
                .where(p -> p.eq(EmployeeDto::getLineId, lineId))
                .groupBy(e -> e.column(Employee::getPost))
                .select(Map.class, p -> p.column(EmployeeDto::getPost))
                .toList();
        var ids = emplList.stream().map(p -> (Long) p.get("post")).collect(Collectors.toList());
        var page = queryHelper.entityQuery(PostCfg.class).where(p -> p.id().in(ids)).toPageResult(pageSort.getPageNum(), pageSort.getPageSize());
        return ResultRows.toRows(page.getTotal(), page.getData());
    }

    /** 人员
     *
     * @param dto
     * @param pageSort
     * @return
     */
    public  ResultRows<EmployeeDto> lineEmplList(EmployeeDto dto,PageSort pageSort) {

        var page = queryHelper.queryable(EmployeeDto.class).whereObject(dto)
                .orderByObject(pageSort)
                .toPageResult(pageSort.getPageNum(), pageSort.getPageSize());
        return ResultRows.toRows(page.getTotal(), page.getData());
    }


    /** 日薪资 time,wage,ratioBaseFinal
     *
     * @param lineId
     * @param pageSort
     * @return
     */
    public ResultRows<DailyWage> lineDailyWage(Long lineId,PageSort pageSort) {
        var ids = queryHelper.entityQuery(Department.class).where(p -> p.lineId().eq(lineId)).selectColumn(d -> d.deptId()).toList();
        var page=queryHelper.entityQuery(DailyWage.class)
                .where(p -> p.deptId().in(ids))
                .groupBy(d -> GroupKeys.of(d.time().format("yyyyMMdd")))
                .select(group -> {
                    var dw = new DailyWageProxy();
                    dw.lineName().set(group.groupTable().lineName().max());
                    dw.time().set(group.groupTable().time().max());
                    dw.wage().set(group.groupTable().wage().sum());
                    dw.ratioBaseFinal().set(group.groupTable().wage().multiply(group.groupTable().ratioBaseFinal()).sum());
                    return dw;
                }).orderByObject(pageSort).toPageResult(pageSort.getPageNum(),pageSort.getPageSize());
        return Result.toRows(page.getTotal(),page.getData());
    }

    /** 产线月工资  line_id,line_name,time,wage_total
     *
     * @param lineId
     * @param pageSort
     * @return
     */
    public ResultRows<lineMonthlyWageDto> lineMonthlyWage(Long lineId,PageSort pageSort) {
        var count = queryHelper.CreateItems(sql, lineId).count().query2Count();
        if (count < 0)
            return ResultRows.toEmpty("");
        var list = queryHelper.CreateItems(sql, lineId)
                .page(pageSort.getPageNum(), pageSort.getPageSize())
                .sqlQuery(lineMonthlyWageDto.class);
                //.queryMap();(ResultRows)

        return  ResultRows.toRows(count, list);

    }
    String sql="SELECT c.line_id, max( c.line_name2 ) line_name, sum( c.wage_total ) wage_total, max( c.TIME ) `time` FROM ( SELECT a.*, b.line_id, b.line_name line_name2 FROM pay_monthly_wage a LEFT JOIN v_employee_info b ON a.user_id = b.user_id WHERE 1 = 1 and b.line_id=? ) c GROUP BY c.line_id, date_format( c.TIME, '%Y%m' ) ORDER BY date_format(c.TIME,'%Y%m') desc,line_id desc";
    String lineDeptMonthlyWageSql="SELECT c.line_id, max( c.line_name2 ) line_name, sum( c.wage_total ) wage_total, max( c.TIME ) `time`, c.dept_id, max(c.dept_name) dept_name FROM ( SELECT a.*, b.line_id, b.line_name line_name2 , b.dept_id, b.dept_name FROM pay_monthly_wage a LEFT JOIN v_employee_info b ON a.user_id = b.user_id WHERE 1 = 1 and b.line_id=? ) c GROUP BY c.line_id, date_format( c.TIME, '%Y%m' ),c.dept_id ORDER BY date_format(c.TIME,'%Y%m') desc,line_id desc";
    /**  班组月薪资  line_id,line_name,time,wage_total,dept_id,dept_name
     *
     */
    public ResultRows<Map> lineDeptMonthlyWage(Long lineId,PageSort pageSort){

        var count = queryHelper.CreateItems(sql, lineId).count().query2Count();
        if (count < 0)
            return ResultRows.toEmpty("");
        var list = queryHelper.CreateItems(lineDeptMonthlyWageSql, lineId)
                .page(pageSort.getPageNum(), pageSort.getPageSize())
                .queryMap();

        return (ResultRows) ResultRows.toRows(count, list);
    }
}
